//
//  MYGAddFriendTableViewCell.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/4/25.
//

import UIKit

class MYGAddFriendTableViewCell: UITableViewCell {
    var isSearch:Bool = false {
        didSet {
            closeButton.isHidden = isSearch
        }
    }
    var relaodData:(()->Void)?
    var model: MYGFriendModel? {
        didSet{
            guard let item = model else {
                return
            }
            
            avatar.sd_setImage(with: URL(string: item.avatar), placeholderImage: UIImage(named: "head"))
            nameLabel.text = item.name
            Label.text = item.hospital
            contentLabel.text = item.desc
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func configUI() {
        contentView.backgroundColor = .rgba(250, 250, 250, 1)
        contentView.addSubview(avatar)
        contentView.addSubview(nameLabel)
        contentView.addSubview(contentLabel)
        contentView.addSubview(Label)
        contentView.addSubview(reButton)
        contentView.addSubview(closeButton)
        
        avatar.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(50)
        }
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(avatar.snp.right).offset(10)
            make.height.equalTo(22.5)
            make.top.equalTo(avatar)
        }
        Label.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right)
            make.centerY.equalTo(nameLabel)
        }
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(avatar)
        }
        
        closeButton.snp.makeConstraints { make in
            make.right.equalToSuperview().inset(8)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(12)
        }
        reButton.snp.makeConstraints { make in
            make.right.equalTo(closeButton.snp.left).offset(-10)
            make.width.equalTo(60)
            make.height.equalTo(23.5)
            make.centerY.equalToSuperview()
        }
    }
    
    
    // MARK: - lazy load
    private lazy var avatar: UIImageView = {
        let imgV = UIImageView()
        imgV.layer.masksToBounds = true
        imgV.layer.cornerRadius = 25
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapAvatar))
        imgV.isUserInteractionEnabled = true
        imgV.addGestureRecognizer(tap)
        return imgV
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(16)
        label.textColor = .rgba(44, 44, 44, 1)
        return label
    }()
    
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(12)
        label.textColor = .rgba(128, 128, 128, 1)
        return label
    }()
    
    private lazy var Label: UILabel = {
        let label = UILabel()
        label.font = .regular(12)
        label.textColor = .rgba(128, 128, 128, 1)
        return label
    }()
    
    private lazy var reButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("添加", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .semibold(13)
        btn.backgroundColor = .rgba(255, 76, 108, 1)
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 3
        btn.addTarget(self, action: #selector(MayKnowData), for: .touchUpInside)
        return btn
    }()
    
    private lazy var closeButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "friend_close"), for: .normal)
        btn.addTarget(self, action: #selector(delMayKnowData), for: .touchUpInside)

        return btn
    }()

}

extension MYGAddFriendTableViewCell {
    
    @objc func tapAvatar() {
        let vc = MYGMicrostationViewController()
        
        var model =  MYGFriendsModel()
        model.id = "\(self.model?.user_id ?? 0)"
        
        var doctorModel = MYGFriendsDoctorModel()
        
        doctorModel.name = self.model!.name
        doctorModel.avatar = self.model!.avatar
        doctorModel.department = self.model!.department
        doctorModel.hospital = self.model!.hospital
        doctorModel.id = self.model!.id
        doctorModel.phone = self.model!.phone
        doctorModel.title = self.model!.title
        
        model.from_doctor = doctorModel
        
        vc.model = model
        self.rootResponsder().navigationController?.pushViewController(vc, animated: true)
    }
    
    @objc func delMayKnowData() {
        MYGLoading.show()
        MMYGNetwork<MYGMessageApi, MYGEmptyModel>.request(target: .delkMayKnow(id: "\(self.model!.id)")) { [weak self] _ in
            MYGLoading.hide()
            self!.relaodData?()
        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
        
    }
    
    @objc func MayKnowData() {
        MYGLoading.show()
        MMYGNetwork<MYGMessageApi,MYGEmptyModel>.request(target: .addDoctorFriend(target_doctor_id: "\(self.model!.id)")) { [weak self] _ in
            MYGLoading.hide()
            self!.relaodData?()
        }  failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
        
    }
}
